"""

【注意：开发中，还没有完成。】

"""
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
)
import os

template = "以下是人类和AI的对话。请在30字以内总结人类提到的身体不适。人类的最后一句话是："

model = ChatOpenAI(
    streaming=False,
    verbose=True,
    callbacks=[],
    openai_api_key="EMPTY",
    openai_api_base=f"http://127.0.0.1:6006/v1",
    model_name="chatglm2-6b-int4",
    temperature=1.0,
    openai_proxy=None,
)


def analyze_symptom(xinput, xlog, xuser):
    print(xlog)
    chat_prompt = ChatPromptTemplate.from_messages([
        ("system", template),
    ])
    for xpair in xlog:
        chat_prompt.append(('human', xpair[0]))
        chat_prompt.append(('ai', xpair[1]))
        # chat_prompt.append(('ai', '收到。'))
    chat_prompt.append(('human', xinput))
    # chat_prompt.append(('ai', '好的，我会分析我们的会话，看看你有什么不适。'))
    # chat_prompt.append(('human', '只简单回答有什么不适即可，如果你不清楚就回答“不知道”。'))
    chain = LLMChain(prompt=chat_prompt, llm=model, verbose=True)
    xres = chain({})
    print('xres:', xres)
    return xres


if '__main__' == __name__:
    import pymongo as pm
    from PyCmpltrtok.common import sep
    from PyCmpltrtok.util_mongo import get_history
    from python_nlp.wechat.x00350_chatglm2_redis_history.daemon import MONGODB_NAME
    
    username = 'oW1QH6VuJHlpGugb0XrzlNEV2wBQ'
    
    # 连接Mongodb
    mongo = pm.MongoClient('127.0.0.1', int(os.environ['M_PORT']), serverSelectionTimeoutMS=3000)
    mdb = mongo[MONGODB_NAME]
    get_history(mdb, 'user_xxxx')  # try it

    xlog = get_history(mdb, username)

    xres = analyze_symptom('我好像是感冒了。', xlog, username)
    sep('RESULT')
    print(f'|{xres}|')